﻿using System;
using Microsoft.Practices.ObjectBuilder;
using System.Web.UI.WebControls;

namespace AP.LUNA.Shell.Views
{
    public partial class AdministrarDesempenos : Microsoft.Practices.CompositeWeb.Web.UI.UserControl, IAdministrarDesempenosView
    {
        private AdministrarDesempenosPresenter _presenter;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this._presenter.OnViewInitialized();
            }
            this._presenter.OnViewLoaded();
        }

        [CreateNew]
        public AdministrarDesempenosPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }

        // TODO: Forward events to the presenter and show state to the user.
        // For examples of this, see the View-Presenter (with Application Controller) QuickStart:
        //	

        #region Eventos Controles
        protected void GridView1_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            int index = int.Parse(e.CommandArgument.ToString());
            this.IdAsignatura = int.Parse(gvAsignaturas.DataKeys[index].Value.ToString());
            switch (e.CommandName)
            {
                case "Temas":
                    this.Presenter.RegistrarDesempeño();
                    break;
                case "Desempeno":
                    this.AdministrarDesempenosComprension1.IdAsignatura = this.IdAsignatura;
                    this.AdministrarDesempenosComprension1.CargarDesempeños();
                    this.IndexView = 3;
                    break;
            }
        }

        protected void gvDesempenos_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            this.Operacion = e.CommandName;
            switch (this.Operacion)
            {
                case "Nuevo":
                    this.IndexView = 2;
                    Limpiar();
                    break;
                case "Editar":
                    int index = int.Parse(e.CommandArgument.ToString());
                    this.IdTema = Guid.Parse(gvDesempenos.DataKeys[index].Value.ToString());
                    this.Presenter.CargarEditar();
                    break;
                case "Eliminar":
                    break;
            }
        }

        protected void lkbGuardar_Click(object sender, EventArgs e)
        {
            switch (this.Operacion)
            {
                case "Nuevo":
                    this.Presenter.Guardar();
                    break;
                case "Editar":
                    this.Presenter.Actualizar();
                    break;
            }

        }
        protected void lkbVolver_Click(object sender, EventArgs e)
        {
            this.IndexView = 0;
        }
        protected void lkbCancelar_Click(object sender, EventArgs e)
        {
            this.IndexView = 1;
        }
        protected void AdministrarDesempenosComprension1_OnClickVolver(object sender, EventArgs e)
        {
            this.IndexView = 0;            
        }
        #endregion

        #region Implementacion Interfaz

        public void Limpiar()
        {
            WebUtil.CleanControl(this);
        }

        public string msgResult
        {
            set { WebUtil.MostrarMensaje("LUNA 2.0", value); }
        }

        public string Tema
        {
            get
            {
                return this.txtTema.Text;
            }
            set
            {
                this.txtTema.Text = value;
            }
        }

        public int Porcentaje
        {
            get
            {
                return Convert.ToInt16(this.txtPorcentaje.Text);
            }
            set
            {
                this.txtPorcentaje.Text = value.ToString();
            }
        }

        public int Periodo
        {
            get
            {
                return int.Parse(this.ddlPeriodo.SelectedValue);
            }
            set
            {
                this.ddlPeriodo.SelectedValue = value.ToString();
            }
        }

        public object ListDesempenos
        {
            set
            {
                this.gvDesempenos.DataSource = value;
                this.gvDesempenos.DataBind();
            }
        }


        public object ListAsignaturas
        {
            set
            {
                this.gvAsignaturas.DataSource = value;
                this.gvAsignaturas.DataBind();
            }
        }

        public Guid IdTema
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int IdAsignatura
        {
            get
            {
                return (int)ViewState["IdAsignatura"];
            }
            set
            {
                ViewState["IdAsignatura"] = value;
            }
        }


        public int IndexView
        {
            set { this.MultiView1.ActiveViewIndex = value; }
        }


        public object ListPeriodos
        {
            set
            {
                this.ddlPeriodo.DataSource = value;
                this.ddlPeriodo.DataBind();
            }
        }

        public string Operacion
        {
            get
            {
                return (string)ViewState["Operacion"];
            }
            set
            {
                ViewState["Operacion"] = value;
            }
        }

        #endregion

    }
}

